import time
from telnetlib import EC

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from common.Utils import ChatDriver

class ChatDetails:
    url=""
    driver=""
    def __init__(self):
        self.url="http://127.0.0.1:8080/client.html"
        self.driver=ChatDriver.driver
        self.driver.get(self.url)
    #登录状态下聊天详情页
    def DetailsTestByLogin(self):
        self.driver.find_element(By.CSS_SELECTOR,"#session-list > li.selected").click()
        #检查元素
        #聊天对象名称
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.right > div.title")
        #账号名称
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.right > div.message-show > div:nth-child(1) > div > h4")
        #账号聊天内容
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.right > div.message-show > div:nth-child(1) > div > p")
        #聊天对象名称
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.right > div.message-show > div:nth-child(3) > div > h4")
        #对象聊天内容
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.right > div.message-show > div:nth-child(3) > div > p")
        #添加屏幕截图
        ChatDriver.getScreeShot()
        self.driver.back()

    #未登录状态下聊天详情页
    def DetailsTestNotByLogin(self):
        newurl = "http://127.0.0.1:8080/client.html"
        self.driver.get(newurl)
        alert = self.driver.switch_to.alert
        text = alert.text
        assert text == "当前用户未登录!"
        # 添加屏幕截图
        ChatDriver.getScreeShot()
        alert.accept()
        # 回到登录界面
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 切换到警告框
        # 等待警告框出现
        try:
            WebDriverWait(self.driver, 10).until(EC.alert_is_present())
            alert = self.driver.switch_to.alert
        except Exception as e:
            print(f"警告框未出现或已关闭: {e}")
        alert = self.driver.switch_to.alert
        alert.accept()

